/**
 * 
 */
package id.ac.gunadarma.paracorp.viewmodel;

import id.ac.gunadarma.paracorp.common.Constant;
import id.ac.gunadarma.paracorp.model.TranslatedSentence;
import id.ac.gunadarma.paracorp.services.TranslatedSentenceService;

import java.util.List;

import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.GlobalCommand;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.select.annotation.VariableResolver;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zul.event.PagingEvent;

/**
 * @author rezalesmana
 *
 */
@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
public class TranslatedSentenceVM {
	
	private String language = Constant.INDONESIA;
	
	private List<?> listSentences;
	
	private TranslatedSentence selectedItem; 
	
	private long totalSize = 0;
	
	private int activePage = 0;
	
	private int pageSize = 0;
	
	@WireVariable
	private TranslatedSentenceService translatedSentenceService;
	
	@Init
	@NotifyChange({"totalSize", "listSentences"})
    public void init( @BindingParam( "language" ) String language, @BindingParam("pageSize") int pageSize){
		if( Constant.isValidConstant( language ) ) {
			this.setLanguage( language );
		}
		this.setPageSize(pageSize);
		this.reloadPage();
    }
	
	@Command
	@NotifyChange({"totalSize", "listSentences"})
	public void loadPage(@ContextParam(ContextType.TRIGGER_EVENT) PagingEvent event) {
		this.setActivePage(event.getActivePage());
		this.reloadPage();
	}

	@GlobalCommand
	@NotifyChange({"totalSize", "listSentences"})
	public void uploadFinished(@BindingParam("language") String language) {
		if(this.getLanguage().equals(language)) {
			this.reloadPage();
		}
	}
	
	public void reloadPage() {
		this.setTotalSize(this.getService().getTotalSize(this.getLanguage()));
		this.setListSentences(this.getService().findAll(this.getActivePage(), this.getPageSize(), this.getLanguage()));
	}
	
	public String getLanguage() {
		return language;
	}

	public void setLanguage( String language ) {
		this.language = language;
	}

	public List<?> getListSentences() {
		return listSentences;
	}

	public void setListSentences( List<?> listSentences ) {
		this.listSentences = listSentences;
	}

	public TranslatedSentence getSelectedItem() {
		return selectedItem;
	}

	public void setSelectedItem( TranslatedSentence selectedItem ) {
		this.selectedItem = selectedItem;
	}
	
	public long getTotalSize() {
		return totalSize;
	}

	public void setTotalSize(long totalSize) {
		this.totalSize = totalSize;
	}

	public int getActivePage() {
		return activePage;
	}

	public void setActivePage(int activePage) {
		this.activePage = activePage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	private TranslatedSentenceService getService() {
		return translatedSentenceService;
	}

}
